package com.zyuc.spark.service.example

import com.alibaba.fastjson.JSONObject
import com.zyuc.spark.service.SparkServerTrait
import org.apache.hadoop.fs.FileSystem
import org.apache.spark.sql.SparkSession

/**
  * Created by zhoucw on 18-8-9 下午4:44.
  */
case object AccessLogServer extends SparkServerTrait{
  /**
    * 处理服务请求
    *
    * @param spark
    * @param fileSystem
    * @param params
    * @return
    */
  override def dealServerLine(spark: SparkSession, fileSystem: FileSystem, params: JSONObject): String = {
    val appName = params.getString("appName")
    val list = List(
      ("49.7.4.16", "118.191.0.131", "2018-08-01 23:51:18", "317"),
      ("182.247.252.80", "221.228.79.239", "2018-08-02 22:51:23", "345"),
      ("182.247.252.78", "118.191.0.131", "2018-08-03 21:52:34", "1005"),
      ("183.205.32.86", "221.228.112.80", "2018-08-01 13:51:56", "242"),
      ("59.57.90.202", "221.228.78.4", "2018-08-01 13:54:22", "1009"),
      ("113.248.158.232", "221.228.112.56", "2018-08-01 12:51:39", "317")
    )

    import spark.implicits._
    val rdd = spark.sparkContext.makeRDD(list)
    val df = rdd.toDF("srcip", "destip", "accesstime", "hid")
    val cnt = df.count()
    df.show()
    s"appName:${appName} cnt:${cnt} result:success"
  }
}
